﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DoeObjects;
using System.Collections.Generic;

namespace DoeLibsTest
{
    [TestClass]
    public class Notification_Test
    {
        [TestInitialize]
        public void initializeDatabaseConnection()
        {
            TestContext.initialize();
        }

        [TestMethod]
        public void Test_markAsRead()
        {
            Notification not = Helper.createNotification(NotificationType.REGISTRATION_ACCEPT_REQUEST, true, null);

            Assert.IsFalse(not.Read, "Notification should be unread after it was created...");

            Assert.IsTrue(Notification.markNotificationAsRead(not.NotificationId), "could not mark notification as read");

            Notification notLoaded = Notification.getById(not.NotificationId);
            Assert.IsTrue(notLoaded.Read, "notification was marked as read before");
        }

        [TestMethod]
        public void Test_getAllForUser()
        {
            User u1 = Helper.createUserObject(true, true);
            User u2 = Helper.createUserObject(false, true);

            Assert.AreEqual(0, Notification.getAllForUser(u1.UserId).Count, "user1 should not have any notifications");
            Assert.AreEqual(0, Notification.getAllForUser(u2.UserId).Count, "user2 should not have any notifications");

            //create notifications for user1
            Notification n1 = Helper.createNotification(NotificationType.REGISTRATION_ACCEPT_REQUEST, true, null, u1);
            Notification n2 = Helper.createNotification(NotificationType.TITLE_RESERVATION, true, null, u1);
            Notification n3 = Helper.createNotification(NotificationType.UNKNOWN, true, u2, u1);

            Assert.AreEqual(3, Notification.getAllForUser(u1.UserId).Count, "user1 should have 3 notifications");
            Assert.AreEqual(0, Notification.getAllForUser(u2.UserId).Count, "user2 should not have any notifications");

            //create notifications for user2
            Notification n4 = Helper.createNotification(NotificationType.UNKNOWN, true, null, u2);

            Assert.AreEqual(3, Notification.getAllForUser(u1.UserId).Count, "user1 should have 3 notifications");
            Assert.AreEqual(1, Notification.getAllForUser(u2.UserId).Count, "user2 should have 1 notification");

            //set read status
            n1.Read = true;
            n4.Read = true;
            Assert.IsTrue(n1.save(), "could not update notification1");
            Assert.IsTrue(n4.save(), "could not update notification4");

            Assert.AreEqual(3, Notification.getAllForUser(u1.UserId).Count, "user1 should have 3 notifications");
            Assert.AreEqual(1, Notification.getAllForUser(u2.UserId).Count, "user2 should have 1 notification");
        }

        [TestMethod]
        public void Test_getAllForUserUnread()
        {
            User u1 = Helper.createUserObject(true, true);
            User u2 = Helper.createUserObject(false, true);

            Assert.AreEqual(0, Notification.getAllForUser(u1.UserId, true).Count, "user1 should not have any unread notifications");
            Assert.AreEqual(0, Notification.getAllForUser(u2.UserId, true).Count, "user2 should not have any unread notifications");

            //create notifications for user1
            Notification n1 = Helper.createNotification(NotificationType.REGISTRATION_ACCEPT_REQUEST, true, null, u1);
            Notification n2 = Helper.createNotification(NotificationType.TITLE_RESERVATION, true, null, u1);
            Notification n3 = Helper.createNotification(NotificationType.UNKNOWN, true, u2, u1);

            //create notifications for user2
            Notification n4 = Helper.createNotification(NotificationType.UNKNOWN, true, null, u2);

            //set read status
            n1.Read = true;
            n4.Read = true;
            Assert.IsTrue(n1.save(), "could not update notification1");
            Assert.IsTrue(n4.save(), "could not update notification4");

            Assert.AreEqual(2, Notification.getAllForUser(u1.UserId, true).Count, "user1 should have 2 unread notifications");
            Assert.AreEqual(0, Notification.getAllForUser(u2.UserId, true).Count, "user2 should have 0 unread notifications");
        }

        [TestMethod]
        public void Test_getAllForUserRead()
        {
            User u1 = Helper.createUserObject(true, true);
            User u2 = Helper.createUserObject(false, true);

            Assert.AreEqual(0, Notification.getAllForUser(u1.UserId, true).Count, "user1 should not have any unread notifications");
            Assert.AreEqual(0, Notification.getAllForUser(u2.UserId, true).Count, "user2 should not have any unread notifications");

            //create notifications for user1
            Notification n1 = Helper.createNotification(NotificationType.REGISTRATION_ACCEPT_REQUEST, true, null, u1);
            Notification n2 = Helper.createNotification(NotificationType.TITLE_RESERVATION, true, null, u1);
            Notification n3 = Helper.createNotification(NotificationType.UNKNOWN, true, u2, u1);

            //create notifications for user2
            Notification n4 = Helper.createNotification(NotificationType.UNKNOWN, true, null, u2);

            //set read status
            n1.Read = true;
            n2.Read = true;
            n4.Read = true;
            Assert.IsTrue(n1.save(), "could not update notification1");
            Assert.IsTrue(n2.save(), "could not update notification2");
            Assert.IsTrue(n4.save(), "could not update notification4");

            Assert.AreEqual(2, Notification.getAllForUser(u1.UserId, false).Count, "user1 should have 2 read notifications");
            Assert.AreEqual(1, Notification.getAllForUser(u2.UserId, false).Count, "user2 should have 1 read notification");
        }
    }
}
